package com.cennavi.minenavidemo.activity.poi;

import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DistrictActivity extends AppCompatActivity implements MapPresenter.OnMapViewListener,
        PoiPresenter.OnPoiSearchListener{

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    private MineMap mMineMap;
    @BindView(R.id.etDistrict)
    EditText etDistrict;
    @BindView(R.id.result)
    TextView result;
    @BindView(R.id.btnSearch)
    Button btnSearch;
    private MapPresenter mMapPresenter;
    private PoiPresenter mPoiPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district);
        mUnbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(DistrictActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(this);
        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(DistrictActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initDistrictSearch();
    }

    @OnClick(R.id.btnSearch)
    public void districtSearch() {
        String district = etDistrict.getText().toString();

        if (TextUtils.isEmpty(district)) {
            Toast.makeText(this, "请输入行政区", Toast.LENGTH_LONG).show();
            return;
        }

        mPoiPresenter.startDistrictSearch(district);
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.onPause();
        }

        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mMapView != null) {
            mMapView.onResume();
        }

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (mMapView != null) {
            mMapView.onDestroy();
        }

        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {

    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {

    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {
        result.setText("name: " + districtItem.getName() + " , center: " + districtItem.getCenter());
    }

    @Override
    public void onGetInputtips(List<Tip> lists) {

    }

    @Override
    public void onMapReady() {
        mMineMap = mMapPresenter.getMineMap();
        Point point = Tools.latLngToPoint(new LatLng(39.984059, 116.307771));
        mMineMap.setPointToCenter(point.x, point.y);
        mMineMap.setZoomLevel(11);
    }

    @Override
    public void onswitchLocation(int imageLevel) {

    }
}
